/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gmail;

import UI.KhoaBiMat;
import UI.Login;
import UI.Send;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JOptionPane;

/**
 *
 * @author Envy
 */
public class GuiEmail {

    private String sendingHost;
    private int sendingPort;
    private String from;
    private String to[];
    private String subject;
    private String text;
    private byte[] textMaHoa;

    public Boolean sendGmail(String receiver, String subject, String text, File fileAttach) {

        // This will send mail from -->sender@gmail.com to -->receiver@gmail.com

        try {
            // This will send mail from -->sender@gmail.com to -->receiver@gmail.com            
            this.from = Login.user;
            this.to = receiver.split(",");
            this.subject = subject;
            this.text = text;

            InternetAddress fromAddress = new InternetAddress(this.from);
            InternetAddress[] toAddress = new InternetAddress[this.to.length];
            for (int i = 0; i < toAddress.length; i++) {
                toAddress[i] = new InternetAddress(this.to[i]);

            }
            // For a Gmail account--sending mails-- host and port shold be as follows

            MimeMessage message = new MimeMessage(Login.session1);
            message.setFrom(fromAddress);
            message.setRecipients(RecipientType.TO, toAddress);
            message.setSubject(this.subject);

            DataSource source = new FileDataSource(fileAttach);
            MimeMultipart multi = new MimeMultipart();

            MimeBodyPart body1 = new MimeBodyPart();
            body1.setText(this.text);
            multi.addBodyPart(body1);

            if (KhoaBiMat.key != null) {
                MimeBodyPart body2 = new MimeBodyPart();
                body2.setDataHandler(new DataHandler(KhoaBiMat.key, "doixung"));
                multi.addBodyPart(body2);
            }

            if (fileAttach != null) {
                MimeBodyPart body2 = new MimeBodyPart();
                body2.setDataHandler(new DataHandler(source));
                body2.setFileName(fileAttach.getName());
                multi.addBodyPart(body2);
            }

            message.setContent(multi);
            message.saveChanges();


            Login.transport1 = Login.session1.getTransport("smtps");
            Login.transport1.connect("smtp.gmail.com", 465, Login.user, Login.pass);

            Login.transport1.sendMessage(message, message.getAllRecipients());
            JOptionPane.showMessageDialog(null, "Mail sent successfully ...", "Mail sent", JOptionPane.PLAIN_MESSAGE);
            return true;

        } catch (MessagingException ex) {
            Logger.getLogger(GuiEmail.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Sending email to: " + to + " failed !!!", "Falied to Send!!!", JOptionPane.ERROR_MESSAGE);
            return false;
        }

    }

    public Boolean sendGmailMaHoa(String receiver, String subject, byte[] textMaHoa, File fileAttach) throws FileNotFoundException, IOException {

        // This will send mail from -->sender@gmail.com to -->receiver@gmail.com

        try {
            // This will send mail from -->sender@gmail.com to -->receiver@gmail.com            
            this.from = Login.user;
            this.to = receiver.split(",");
            this.subject = subject;
            this.textMaHoa = textMaHoa;

            InternetAddress fromAddress = new InternetAddress(this.from);
            InternetAddress[] toAddress = new InternetAddress[this.to.length];
            for (int i = 0; i < toAddress.length; i++) {
                toAddress[i] = new InternetAddress(this.to[i]);

            }
            // For a Gmail account--sending mails-- host and port shold be as follows

            MimeMessage message = new MimeMessage(Login.session1);
            message.setFrom(fromAddress);
            message.setRecipients(RecipientType.TO, toAddress);
            message.setSubject(this.subject);

            DataSource source = new FileDataSource(fileAttach);
            MimeMultipart multi = new MimeMultipart();

            MimeBodyPart body1 = new MimeBodyPart();
            FileOutputStream fos = new FileOutputStream("ecrypted");
            fos.write(this.textMaHoa);
            fos.close();
            DataSource ds = new FileDataSource("ecrypted");
            //Add attachment    
            body1.setDataHandler(new DataHandler(ds));
            multi.addBodyPart(body1);
//
            if (fileAttach != null) {
                MimeBodyPart body2 = new MimeBodyPart();
                body2.setDataHandler(new DataHandler(source));
                body2.setFileName(fileAttach.getName());
                multi.addBodyPart(body2);
            }

            message.setContent(multi);
            message.saveChanges();

            Login.transport1 = Login.session1.getTransport("smtps");
            Login.transport1.connect("smtp.gmail.com", 465, Login.user, Login.pass);

            Login.transport1.sendMessage(message, message.getAllRecipients());
            JOptionPane.showMessageDialog(null, "Mail sent successfully ...", "Mail sent", JOptionPane.PLAIN_MESSAGE);
            return true;

        } catch (MessagingException ex) {
            Logger.getLogger(GuiEmail.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Sending email to: " + to + " failed !!!", "Falied to Send!!!", JOptionPane.ERROR_MESSAGE);
            return false;
        }

    }
}
